
#include "../User/TASK/task.h"
#include "../User/BSP/gd25q64/gd25q64.h"
#include "../User/BSP/RTC/rtc.h"


TASKIndex_struct TASKIndex;

extern TASKInfo_S_struct TASKInfo_S;
extern TASKInfo_I_struct TASKInfo_I;

extern RTC_union RtcDate;

/*!
    \brief      vtaskIndexInit
    \param[in]  none
    \param[out] none
    \retval     none
*/
void vtaskIndexInit(void)
{

    TASKIndex.tasknum = 0;

    vgd25q64ReadData(TASKInfo_I.num, PosGD25Q64_TaskInfo_num_I, 2);
    vdatastorageTaskIInit();
    uint8_t taskInfo_I_group = vdatastorageMergeArrayto16(TASKInfo_I.num) / (TASKInfo_I_Eachgroup_num);

    for(uint8_t group = 0; group <= taskInfo_I_group; group++)
    {

        vgd25q64ReadData((uint8_t *)TASKInfo_I.Info, PosGD25Q64_TaskInfo_Task_I + (group * TASKInfo_I_Eachgroup_num * TASKInfo_I_union_Byte_length), TASKInfo_I_Eachgroup_num * TASKInfo_I_union_Byte_length);
        for(uint32_t i = 0; i < (TASKInfo_I_Eachgroup_num); i++)
        {
            if(((TASKInfo_I.Info[i].info.index[0] == 0xFF) && (TASKInfo_I.Info[i].info.index[1] == 0xFF)) || \
                    ((TASKInfo_I.Info[i].info.index[0] == 0x00) && (TASKInfo_I.Info[i].info.index[1] == 0x00))
              )
            {
                if(TASKIndex.tasknum >= vdatastorageMergeArrayto16(TASKInfo_I.num))
                {
                    break;
                }
            }
            else
            {
                TASKIndex.task[TASKIndex.tasknum].info.type = 0x01;
                vdatastorageCopyArray(TASKInfo_I.Info[i].info.index, TASKIndex.task[TASKIndex.tasknum].info.index, 2);
                vdatastorageCopyArray(TASKInfo_I.Info[i].info.starttime, TASKIndex.task[TASKIndex.tasknum].info.starttime, 4);
                TASKIndex.tasknum++;
            }
        }
    }

    vgd25q64ReadData(TASKInfo_S.num, PosGD25Q64_TaskInfo_num_S, 2);
    vdatastorageTaskSInit();
    uint8_t taskInfo_S_group = vdatastorageMergeArrayto16(TASKInfo_S.num) / (TASKInfo_S_Eachgroup_num);

    for(uint8_t group = 0; group < taskInfo_S_group; group++)
    {

        vgd25q64ReadData((uint8_t *)TASKInfo_S.Info, PosGD25Q64_TaskInfo_Task_S + (group * TASKInfo_S_union_Byte_length * TASKInfo_S_Eachgroup_num), TASKInfo_S_union_Byte_length * TASKInfo_S_Eachgroup_num);

        for(uint32_t i = 0; i < (TASKInfo_I_Eachgroup_num); i++)
        {
            if(((TASKInfo_S.Info[i].info.index[0] == 0xFF) && (TASKInfo_S.Info[i].info.index[1] == 0xFF)) || \
                    ((TASKInfo_S.Info[i].info.index[0] == 0x00) && (TASKInfo_S.Info[i].info.index[1] == 0x00))
              )
            {
                if((TASKIndex.tasknum >= vdatastorageMergeArrayto16(TASKInfo_S.num)))
                {
                    break;
                }
            }
            else
            {
                TASKIndex.task[TASKIndex.tasknum].info.type = 0x00;
                vdatastorageCopyArray(TASKInfo_S.Info[i].info.index, TASKIndex.task[TASKIndex.tasknum].info.index, 2);
                vdatastorageCopyArray(TASKInfo_S.Info[i].info.starttime, TASKIndex.task[TASKIndex.tasknum].info.starttime, 4);
                TASKIndex.tasknum++;
            }
        }
    }
}


uint32_t currentTask;
/*!
    \brief      vtaskExecution
    \param[in]  none
    \param[out] none
    \retval     none
*/
void vtaskExecution(void)
{
    //have task
    if(TASKIndex.tasknum > 0)
    {
        for(currentTask = 0; currentTask < TASKIndex.tasknum; currentTask++)
        {
            //this task no carry out
            if(TASKIndex.task[currentTask].info.status != 0x01)
            {
                //->  time ->hour
                if(TASKIndex.task[currentTask].info.starttime[1] >= RtcDate.Info.hour)
                {
                    //-> time ->minute
                    if(TASKIndex.task[currentTask].info.starttime[2] >= RtcDate.Info.minute)
                    {
                        //-> time ->second
                        if(TASKIndex.task[currentTask].info.starttime[3] >= RtcDate.Info.second)
                        {
                            //is time go carry out task
                            TASKIndex.task[currentTask].info.status = 0x01;
                            //






                        }
                    }




                }
            }

        }
    }

}



